<template>
	<view class="searchMsgBox">


		<view class="header">
			<uni-section title="控制清除/取消按钮" subTitle="使用 clearButton 属性设置清除按钮" type="line">
				<uni-search-bar radius="5" placeholder="请输入搜索关键词" clearButton="always" cancelButton="always"
					@confirm="search" @cancel="cancel" />
			</uni-section>
		</view>

		<view class="body" v-if="isFind">
			<view class="nav">
				<ul>
					<li v-for="(item,index) in navList" :key="item.id" :class="{active: index==checked}"
						@click="changeActive(index)">{{item.title}}
					</li>
				</ul>
			</view>
			<view class="resultBox">
				<view class="msgBox" v-show="checked==0">
					<messagesBox :messagesList="resultMsgsList" v-if="resultMsgsList.length!=0"></messagesBox>
					<view class="result_no" v-else>
						{{tipMsg}}
					</view>
				</view>
				<view class="userBox" v-show="checked==1">
					<simpleUserInfo v-if="resultUserList.length!=0" :simpleInfoList="resultUserList"></simpleUserInfo>
					<view class="result_no" v-else>
						{{tipUser}}
					</view>
				</view>
			</view>

		</view>

	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		data() {
			return {
				navList: [{
						title: '帖子',
						id: 0
					},
					{
						title: '用户',
						id: 1
					}
				],
				keyword: '',
				resultMsgsList: [],
				resultUserList: [],
				pageMsg: 1,
				size: 10,
				date: '2022-12-31',
				mid: '',
				isLoading: false,
				checked: 0,
				isFind: false,
				pageUser: 1,
				tipMsg: '',
				tipUser: ''

			};
		},
		computed: {
			...mapState('my_Info', ['userInfo'])
		},
		methods: {

			changeActive(index) {
				this.checked = index
				switch (index) {
					case 0:
						break
					case 1:
						// if (this.resultUserList.length == 0) {
						this.getSearchUserList()
						// }
						break
				}
			},

			search(res) {
				// uni.showToast({
				// 	title: '搜索：' + res.value,
				// 	icon: 'none'
				// })
				this.keyword = res.value
				this.isFind = true
				this.resultMsgsList = []
				this.pageMsg = 1,
					this.mid = ''
				this.date = '2099-12-31'
				this.getSearchMsgsList()

				this.pageUser = 1
				this.resultUserList = []
				this.getSearchUserList()

			},
			input(res) {
				console.log('----input:', res)
			},
			clear(res) {
				// uni.showToast({
				// 	title: 'clear事件，清除值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			blur(res) {
				// uni.showToast({
				// 	title: 'blur事件，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			focus(e) {
				// uni.showToast({
				// 	title: 'focus事件，输出值为：' + e.value,
				// 	icon: 'none'
				// })
			},
			cancel(res) {
				this.keyword = ''
				// uni.showToast({
				// 	title: '点击取消，输入值为：' + res.value,
				// 	icon: 'none'
				// })
			},
			async getSearchMsgsList() {
				this.isLoading = true
				if (this.resultMsgsList.length != 0 && this.resultMsgsList.length % 10 != 0) {
					uni.showToast({
						title: '没有找到更多数据',
						duration: 1500,
						icon: 'none'
					})
					return
				}

				let {
					data: res
				} = await uni.$http.get('/messages/searchMessages?page=' + this.pageMsg + '&size=' + this.size +
					'&param=' + this.keyword + '&date=' + this.date + '&mid=' + this.mid + '&uid=' + this.userInfo
					.uid)

				this.isLoading = false
				if (res.code == 2000) {
					if (res.data.length == 0) {
						this.tipMsg = '暂无数据'
						uni.showToast({
							title: '没有更多数据',
							duration: 1500,
							icon: 'none'
						})
					} else {
						this.resultMsgsList = [...this.resultMsgsList, ...res.data]
						this.date = res.data[res.data.length - 1].date
						this.mid = res.data[res.data.length - 1].mid
					}
				} else {
					uni.showToast({
						title: '数据加载出错',
						duration: 1500,
						icon: 'error'
					})
				}

			},
			async getSearchUserList() {
				this.isLoading = true
				if (this.resultUserList.length != 0 && this.resultUserList.length % 10 != 0) {

					uni.showToast({
						title: '没有找到更多数据',
						duration: 1500,
						icon: 'none'
					})
					return
				}

				let {
					data: res
				} = await uni.$http.get('/users/searchUser?page=' + this.pageUser + '&size=' + this.size +
					'&nickname=' + this.keyword + '&uid=' + this.userInfo
					.uid)

				this.isLoading = false
				if (res.code == 2000) {
					if (res.data.length == 0) {
						this.tipUser = '暂无数据哦'
						uni.showToast({
							title: '没有更多数据',
							duration: 1500,
							icon: 'none'
						})
					} else {
						this.resultUserList = [...this.resultUserList, ...res.data]
					}
				} else {
					uni.showToast({
						title: '数据加载出错',
						duration: 1500,
						icon: 'error'
					})
				}

			}
		},
		onBackPress() {
			// #ifdef APP-PLUS
			plus.key.hideSoftKeybord();
			// #endif
		},
		onReachBottom() {
			switch (this.checked) {
				case 0:
					if (this.isLoading) return
					this.pageMsg++
					this.getSearchMsgsList()
					break
				case 1:
					if (this.isLoading) return
					this.pageUser++
					this.getSearchUserList()
			}
		}

	}
</script>

<style lang="scss">
	.nav ul {
		display: flex;
		// justify-content: space-between;
		// width: 600rpx;

		li {
			padding: 0 4px;
			margin: 0 5px;
			line-height: 30px;
			text-align: center;
			color: #797a7b;
			list-style: none;
		}

		.active {
			color: #2979ff;
			border-bottom: 3px solid #2979ff;
		}
	}


	.result_no {
		display: flex;
		justify-content: center;
		align-items: flex-end;
		color: #b6b6b6;
		width: 100%;
		height: 300px;
		background: url('@/static/images/empty-image-default.png') no-repeat;
		background-size: 50%;
		background-position: 50% 50%;
	}


	.search-result {
		padding-top: 10px;
		padding-bottom: 20px;
		text-align: center;
	}

	.search-result-text {
		text-align: center;
		font-size: 14px;
		color: #666;
	}

	.example-body {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		padding: 0px;
	}

	.uni-mt-10 {
		margin-top: 10px;
	}
</style>
